home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-11-15 | 10.5 KB | 342 lines | [TEXT/ALFA] |
- ## -*-Tcl-*-
- # ###################################################################
- # Vince's Additions - an extension package for Alpha
- #
- # FILE: "electricMenu.tcl"
- # created: 8/3/96 {1:34:42 pm}
- # last update: 5/8/98 {11:02:18 pm}
- # Author: Vince Darley
- # E-mail: <darley@fas.harvard.edu>
- # mail: Division of Applied Sciences, Harvard University
- # Oxford Street, Cambridge MA 02138, USA
- # www: <http://www.fas.harvard.edu/~darley/>
- #
- # Handles the electric menu.
- #
- # ###################################################################
- ##
-
- alpha::menu electricMenu 1.3.2 global "•280" {
- # so we don't use the standard proc to build this menu.
- menu::buildProc elec "#"
- namespace eval elec {}
- } {
- elec::rebuildElectricMenu
- } {
- } maintainer {
- "Vince Darley" darley@fas.harvard.edu <http://www.fas.harvard.edu/~darley/>
- } uninstall this-file help {file "ElecCompletions Help"}
-
- alpha::package require elecCompletions 9.0
-
- lunion flagPrefs(Electrics) showElectricsInMenu putTemplatesInMainMenu \
- showElectricKeysInMenu addTemplateManipulators
-
- eval lunion elec::MenuTemplates ""
- lunion elec::LicenseTemplates \
- "none" "copyrightNotice" "allRightsReserved" "allRightsReservedOrg" \
- "seeFileLicenseTerms" "gnuPublicLicense"
-
- # register this proc to be called whenever the mode changes.
- hook::register changeMode elec::rebuildElectricMenu
- hook::register electricBindings elec::BindingsChanged
- # Show mode-dependent electric completions in menu; i.e. include in the
- # menu all items which when typed, followed by cmd-Tab, will complete
- # into some command.
- newPref flag showElectricsInMenu 1 global elec::clearAndBuildElectricMenu
- # Add the list of key-bindings to the bottom of the menu. These are
- # the items you may edit with Global->SpecialKeys, which are used to
- # trigger Completions, Expansions, Template Stop movement etc.
- newPref flag showElectricKeysInMenu 1 global elec::clearAndBuildElectricMenu
- # Add a couple of items to let you create or delete new templates
- newPref flag addTemplateManipulators 0 global elec::clearAndBuildElectricMenu
- # Put all the templates into the main menu rather than in a submenu
- newPref flag putTemplatesInMainMenu 0 global elec::clearAndBuildElectricMenu
- proc elec::BindingsChanged {} {
- global showElectricKeysInMenu
- if $showElectricKeysInMenu {elec::clearAndBuildElectricMenu}
- }
-
- proc elec::getMenuBindings {} {
- global showElectricKeysInMenu keys::specialBindings
- # get menu items which represent the current bindings
- if $showElectricKeysInMenu {
- return [menu::bindingsFromArray keys::specialBindings]
- } else {
- return ""
- }
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "elec::rebuildElectricMenu" --
- #
- # Reasonably clever procedure to construct a Template menu from the
- # ${mode}electrics array on the fly. Works with 'ensemble' completions,
- # putting them in submenus (that's why the code is a little messy; I
- # couldn't dream up a neater method).
- # -------------------------------------------------------------------------
- ##
- proc elec::rebuildElectricMenu {args} {
- set mmode [modeALike]
- if {[cache::exists elecMenu::elec-${mmode}]} {
- cache::read elecMenu::elec-${mmode}
- message ""
- } else {
- global ${mmode}electrics electricMenu showElectricsInMenu
-
- set m [list Menu -n ${electricMenu} -m -p elec::MenuProc]
- # make the menu of electrics if desired
- if {$showElectricsInMenu && [array exists ${mmode}electrics]} {
- set items [lsort [array names ${mmode}electrics]]
- # remove all contractions
- set items [lremove -all -glob $items "*'*"]
- # remove something else (I've forgotten what!)
- regsub -all { [^ ]*\*[^ ]*} " $items " { } items
- set c [set items]
- while {[regexp {\{(\w+) \w+\}} $c all pref]} {
- set c [string range $c [expr {[string last "\{$pref " $c] +2}] end]
- lappend got $pref
- }
- if {[info exists got]} {
- foreach pref $got {
- regsub "(\{${pref} \\w+\} )+" $items \
- "\{Menu -n \" ${pref}\" -m -p elec::MenuProc \{\\0\}\} " items
- }
- }
- } else {
- set items ""
- }
- # make the whole menu
- set items [concat [elec::makeTemplatesMenu $mmode] $items "(-" \
- [elec::getMenuBindings] [list "Add Electric Item"] ]
- global addTemplateManipulators
- if ${addTemplateManipulators} {
- lappend items "Grab Selection" "Insert Old Selection" \
- "Insert In Lines"
- }
- lappend items "Clear Elec Menu Cache"
- lappend m $items
- cache::add elecMenu::elec-${mmode} variable m
- }
- eval $m
- }
-
- proc elec::clearAndBuildElectricMenu {args} {
- cache::deletePat elecMenu::elec-*
- elec::rebuildElectricMenu
- }
-
- proc elec::makeTemplatesMenu {mmode} {
- # make the templates submenu
- global ${mmode}Templates elec::MenuTemplates \
- elec::LicenseTemplates menu::additions putTemplatesInMainMenu
- set m ${elec::MenuTemplates}
- if [info exists ${mmode}Templates] {
- set m [concat $m [set ${mmode}Templates]]
- }
- set m [lsort $m]
- eval lappend m "(-" [lsort [lremove ${elec::LicenseTemplates} "none"]]
- if [info exists menu::additions(elec)] {
- foreach i [set menu::additions(elec)] {
- eval lappend m "(-" [lrange $i 2 end]
- }
- }
- lappend m "(-" "addTemplateItem" "removeTemplateItem"
- if $putTemplatesInMainMenu {
- foreach i $m {
- if {[lindex $i 0] == "Menu"} {
- lappend ret $i
- } else {
- lappend ret "[quote::Prettify $i] "
- }
- }
- return $ret
- } else {
- return [list "Menu -n Templates -p elec::userTemplates [list $m]"]
- }
-
- }
-
- proc elec::rebuildTemplatesMenu { {mmode ""} } {
- if {$mmode == ""} {set mmode [modeALike]}
- eval [lindex [elec::makeTemplatesMenu $mmode] 0]
- }
-
- proc elec::userTemplates {menu item} {
- set t [file::$item]
- if {$t != ""} {
- elec::Insertion $t
- }
- }
-
- proc elec::MenuProc {menu item} {
- switch $item {
- "Next Stop Or Indent" {bind::IndentOrNextstop}
- "Prev Stop" { ring::- }
- "nth Stop" {ring::nth}
- "Complete" {bind::Completion}
- "Complete Or Tab" {bind::TabOrComplete}
- "Expand" {bind::Expansion}
- "Next Stop" {ring::+}
- "Real Tab" {insertActualTab}
- "Add Electric Item" {elec::AddItem}
- "Grab Selection" {elec::GrabSelection}
- "Insert Old Selection" {elec::InsertOldSelection}
- "Insert In Lines" {elec::InsertInLines}
- "Clear All Stops" {ring::clear}
- "Clear Elec Menu Cache" {elec::clearAndBuildElectricMenu}
- default {
- if {[regexp {(.*) $} $item "" item]} {
- set item [join $item ""]
- elec::userTemplates $menu \
- [string tolower [string index $item 0]][string range $item 1 end]
- } else {
- insertText $item
- bind::Completion
- }
- }
- }
- }
-
- proc elec::AddItem {} {
- global mode
- if {$mode == ""} { beep ; message "No mode set…" ; return }
- global ${mode}electrics
- set e [prompt "Enter the electric item for '$mode' mode:" ""]
- if {$e == ""} {return}
- set default [file::_varValue ${mode}electrics($e)]
- #[file::_getDefault "Do you want to start with this as the template?"]
- set value [getline "Enter the electric extension, using •prompt•, \\r \\\{, \\\} etc" $default]
- if {$value != ""} {
- if {[string length $value] > 210} {
- alertnote "Alpha unfortunately truncates direct entry to about 200 characters, however you can add directly to arrdefs.tcl"
- }
- eval set ${mode}electrics($e) \"$value\"
- addArrDef ${mode}electrics $e [set ${mode}electrics($e)]
- cache::deletePat elecMenu::elec-[modeALike]
- elec::rebuildElectricMenu
- }
- }
-
- proc file::addTemplateItem {} {
- global elec::MenuTemplates mode
- global ${mode}Templates
- set v elec::MenuTemplates
- set v [expr {$mode != "" && [dialog::yesno \
- "Is this item '$mode' mode-specific (otherwise I'll make it global)?"] \
- ? "${mode}Templates" : "elec::MenuTemplates"}]
- set e [join [prompt "Enter the new template menu item name:" ""] ""]
- if {$e == ""} {return}
- set e [string tolower [string index $e 0]][string range $e 1 end]
- set default [file::_getDefault "Do you want to start with this as the template?" t]
- set t "\r"
- append t "proc file::${e} \{\} \{\r"
- append t "\t# You must fill this in\r"
- append t $default
- append t "\r\r\telec::Insertion \$t\r\}\r"
- addUserLine $t
- lappend $v $e
- lappend modifiedVars $v
- elec::rebuildTemplatesMenu
- regsub -all "\r" $default ";" default
- append default { elec::Insertion $t}
- ;proc file::$e {} $default
- elec::clearAndBuildElectricMenu
- if {[dialog::yesno "I've added a template for the procedure to your 'prefs.tcl'. Do you want to edit it now?"]} {
- global::editPrefsFile
- goto [maxPos]
- }
- return ""
- }
-
- proc file::removeTemplateItem {} {
- global elec::MenuTemplates mode
- global ${mode}Templates
- global modifiedVars
- set tlist ${elec::MenuTemplates}
- catch {set tlist [concat $tlist [set ${mode}Templates]]}
- set l [listpick -p "Which template shall I permanently remove?" \
- [lsort $tlist]]
- if {[set i [lsearch ${elec::MenuTemplates} $l]] != -1} {
- set elec::MenuTemplates [lreplace ${elec::MenuTemplates} $i $i]
- lappend modifiedVars elec::MenuTemplates
- } else {
- set i [lsearch [set ${mode}Templates] $l]
- set ${mode}Templates [lreplace [set ${mode}Templates] $i $i]
- lappend modifiedVars ${mode}Templates
- }
- elec::rebuildTemplatesMenu
- elec::clearAndBuildElectricMenu
- global::editPrefsFile
- set pat "\rproc\[ \t\]+file::"
- append pat $l
- append pat "\[ \t\]+\{\[ \t\]*\}\[ \t\]\{(\r\[^\}\].*)*\r\}\r"
- while 1 {
- set fpos [search -f 1 -r 1 -n $pat 0]
- if {[string compare $fpos ""] == 0} {
- break
- }
- deleteText [lindex $fpos 0] [lindex $fpos 1]
- }
- while 1 {
- set fpos [search -f 1 -r 1 -n "^(\r\r|\n\n)" 0]
- if {[string compare $fpos ""] == 0} {
- break
- }
- replaceText [lindex $fpos 0] [lindex $fpos 1] "\r"
- }
- save
- return ""
- }
-
-
- # procedures below get added to the menu if you set the 'poweruser' flag
- # in "elecBindings.tcl". They make it easy to create large template
- # procedures
- proc elec::GrabSelection {} {
- global __elec::grabbed
- set __elec::grabbed [getSelect]
- }
-
- proc elec::InsertOldSelection {} {
- global __elec::grabbed
- insertText [quote::Insert [set __elec::grabbed]]
- }
-
- proc elec::InsertInLines {} {
- global __elec::grabbed
- insertText [elec::_MakeIntoInsertion ${__elec::grabbed}]
- }
-
- proc elec::_MakeIntoInsertion {t {var "t"}} {
- if {$t == ""} { return $t }
- regsub -all "\n" $t "\r" t
- while 1 {
- set ret [string first "\r" $t]
- if { $ret == -1 } { set ret [string length $t] }
- append b [string range $t 0 $ret]
- if {[string length $b] > 20} {
- while 1 {
- append a "\tappend $var \"[quote::Insert [string range $b 0 59]]\"\r"
- if {[set b [string range $b 60 end]] == ""} {
- break
- }
- }
- }
- set t [string range $t [incr ret] end]
- if {[string length $t] == 0} {
- if {$b != ""} {
- append a "\tappend $var \"$b\"\r"
- }
- break
- }
- }
- return $a
- }
-
-
-
-
-